/*******************************************************************************
 Copyright (c) 2010 Koninklijke Philips N.V.,
 All Rights Reserved.
 
 File name: queue.c
 Description: Source file for the QUEUE component, and provide the utility 
			  functions for the use of queue in the system.
 Project: XXX
 Component name: QUEUE 
 $LastChangedDate:  $
 $Revision:  $
 $Author:  $
 Lighting GBU CFLi
 
 This source code and any compilation or derivative thereof is the
 proprietary information of Koninklijke Philips N.V.
 and is confidential in nature.
 Under no circumstances is this software to be combined with any Open Source
 Software in any way or placed under an Open Source License of any type without
 the express written permission of Koninklijke Philips N.V.
*******************************************************************************/

/****************************************************************************************
* INCLUDE FILES
****************************************************************************************/
#include "queue.h"

/****************************************************************************************
* LOCAL CONSTANTS
****************************************************************************************/

/****************************************************************************************
* LOCAL TYPEDEFS
****************************************************************************************/

/****************************************************************************************
* LOCAL VARIABLES
****************************************************************************************/
static queue_manager_t __queue_manager;
static event_t __event_queue[QUEUE_NUMBER];

/****************************************************************************************
* GLOBAL VARIABLES
****************************************************************************************/

/****************************************************************************************
* LOCAL FUNCTIONS PROTOTYPE
****************************************************************************************/

/****************************************************************************************
* LOCAL MACRO
****************************************************************************************/

/****************************************************************************************
* GLOBAL FUNCTIONS
****************************************************************************************/

/*******************************************************************************
* Function:	
*                   queue_init
* Parameters:	
*                   void
* Returen:
*                   void
* Description: 
*                   Queue inialization.
*******************************************************************************/

void queue_init(void)
{
    __queue_manager.rd_index  = 0; 
    __queue_manager.wr_index  = 0; 
    __queue_manager.data_count = 0; 
}

/*******************************************************************************
* Function:	
*                   queue_add_event
* Parameters:	
*                   event [IN], the event will be added to the queue.
*                   parameter [IN], the parameter for the event.
*                   module_id [IN], the event for which module.
* Returen:
*                   void.
* Description: 
*                   Add an event to the queue.
*******************************************************************************/
void queue_add_event( uint8 event, uint16 parameter, uint8 module_id)
{
    uint8 index;
    
    if (__queue_manager.data_count < NOPOS_QUEUE_SIZE)
    {
        index = __queue_manager.wr_index;
    
        __event_queue[index].module_id    = module_id;
        __event_queue[index].event = event;
        __event_queue[index].param = parameter;

        __queue_manager.wr_index++;
        __queue_manager.data_count++;
        
        if (__queue_manager.wr_index >= QUEUE_NUMBER)
        {
          __queue_manager.wr_index = 0;
        }
    }
    else
    {
        //queue overflow
        while (1);
    }
} 

/*******************************************************************************
* Function:	
*                   queue_get_event
* Parameters:	
*                   p_event [OUT], the pointer point to the event entity.
* Returen:
*                   TURE/FALSE, means get the event from queue or not.
* Description: 
*                   Get an event from the queue.
*******************************************************************************/
uint8 queue_get_event(event_t *p_event)
{
    uint8 result = FALSE;
    uint8 index;

    if (__queue_manager.data_count)
    {
       index = __queue_manager.rd_index;
        
       *p_event =  __event_queue[index];

        __queue_manager.data_count--;
        __queue_manager.rd_index++;
        if (__queue_manager.rd_index >= QUEUE_NUMBER)
          __queue_manager.rd_index = 0;
        result = TRUE;
    }
    
    return result;
} //boUtQueueGetEvent


/****************************************************************************************
* LOCAL FUNCTIONS
****************************************************************************************/

/***************************************************************************************/
